package br.com.digidata.modelo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.digidata.negocio.Laudo;
import br.com.digidata.negocio.Medico;
import br.com.digidata.negocio.Paciente;

public class LaudoDAO {
	private Connection conn;
	
	public boolean insert(Laudo l) {
        conn = ConexaoBD.getConnection();
        boolean retorno = false;

		try {
			PreparedStatement stmt = conn.prepareStatement("INSERT INTO laudo " +
					"(data, queixa, medicacao, diagnostico, medico_id, paciente_id) " +
					"values (?,?,?,?,?,?)");
			
            stmt.setString(1, l.getData());
            stmt.setString(2, l.getQueixa());
            stmt.setString(3, l.getMedicacao());
            stmt.setString(4, l.getDiagnostico());
            stmt.setInt(5, l.getMedico().getId());
            stmt.setInt(6, l.getPaciente().getId());
           
			stmt.executeUpdate();
			
		    retorno = true;
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println("Incluir laudo: " + new java.util.Date() + " " + e);
			retorno = false;
		}
		return retorno;
	}

	public void update(Laudo l) {
		try{
            conn = ConexaoBD.getConnection();
            
            PreparedStatement stmt = conn.prepareStatement("UPDATE laudo SET " +
            		"data = ?, queixa = ?, mediacao = ?, diagnostico = ?, medico_id = ?, paciente_id = ? " +
            		"WHERE id = ? ");
            stmt.setString(1, l.getData());
            stmt.setString(2, l.getQueixa());
            stmt.setString(3, l.getMedicacao());
            stmt.setString(4, l.getDiagnostico());
            stmt.setInt(5, l.getMedico().getId());
            stmt.setInt(5, l.getPaciente().getId());
            stmt.setInt(6, l.getId());
            stmt.executeUpdate();
            
            stmt.close();
            conn.close();
            
        } catch (Exception e){
            System.out.println("Alterar laudo: " + new java.util.Date() + " " + e);
        }
	}
	
	public List<Laudo> retrive(){
        List<Laudo> laudos = new ArrayList<Laudo>();
        
        MedicoDAO medicoDao = new MedicoDAO();
        PacienteDAO pacienteDao = new PacienteDAO();
        
        try {
            conn = ConexaoBD.getConnection();
        	
            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT * FROM laudo");
            
            while (rs.next()) {
            	
            	
            	
            	Medico m = medicoDao.getById(rs.getInt("medico_id"));
            	Paciente p = pacienteDao.getById(rs.getInt("paciente_id"));
                Laudo l = new Laudo(rs.getInt("id"),
                		rs.getString("data"), 
                		rs.getString("queixa"),
                		rs.getString("medicacao"),
                		rs.getString("diagnostico"),
                		m, p);
            	
                laudos.add(l);
            }
            
            rs.close();
            stmt.close();
            conn.close();
            
        } catch (Exception e) {
            System.out.println("Listar agenda: " + new java.util.Date() + " " + e);
        }
        
        return laudos;
    }
}
